package com.ficus.road.maintain.controller;

import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.smartnew.PrimaryRoadSectionDTO;
import com.ficus.road.maintain.core.model.zh.ComplainInfo;
import com.ficus.road.maintain.core.model.zh.ComplainResponse;
import com.ficus.road.maintain.dao.GetRoadPCIAndPQIDao;
import com.ficus.road.maintain.core.model.zh.Complain;
import com.ficus.road.maintain.dao.ComplainDao;
import com.ficus.road.maintain.dao.HitRoadCorrelationDao;
import com.ficus.road.maintain.dao.smartnew.primaryScreening.PrimaryRoadSectionDao;
import com.ficus.road.maintain.service.ComplainService;
import com.ficus.road.maintain.service.ZhongHaiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ficus.road.maintain.service.ZhonghaiClient.ZH_METHOD_COMPLAIN_REPORT;

/**
 * 投诉数据接口
 * @date 2021/8/13 上午11:36
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/complaint")
public class ComplainController {

    @Autowired
    private ComplainDao bridgeInfoDao;
    @Autowired
    private ComplainService complainService;

    /**
     * 投诉接入
     * @return
     */
    @GetMapping("/data")
    public ComplainResponse getComplain(
            @RequestParam("pageNumber") int pageNumber,
            @RequestParam("pageSize") int pageSize) {
        //新建数据投诉返回值集合
        List<Complain> list = new ArrayList<>();
        //前往Mongo获取数据并将获取得数据进行封装
        for (ComplainInfo complainInfo : bridgeInfoDao.get(pageNumber, pageSize)) {
            //新建数据投诉返回值
            Complain complainResponse = new Complain();
            //要素名称
            complainResponse.setElementname(complainInfo.getElementname());
            //案卷描述
            complainResponse.setDescription(complainInfo.getDescription());
            //案卷分类
            complainResponse.setQuestionType(complainInfo.getQuestionType());
            //病害发现图片
            // 使用 | 分割字符串
            String[] parts = complainInfo.getImgUrl().split("\\|");
            complainResponse.setImgUrl(parts[0]);
            //案卷发现地址
            complainResponse.setAddress(complainInfo.getAddress());
            //案卷处置时间
            complainResponse.setReportTime(complainInfo.getReportTime());
            list.add(complainResponse);
        }
        Long count = bridgeInfoDao.getRoadCorrelationCount();
        ComplainResponse complainResponse = new ComplainResponse();
        complainResponse.setDescription(list);
        complainResponse.setCount(count);
        return complainResponse;
    }

    /**
     * 智慧决策道路病害详情弹窗
     *
     * @return
     */
    @PostMapping("/RoadCorrelation")
    public RoadDistressInformation roadCorrelation(
            @RequestParam("pageNumber") int pageNumber,
            @RequestParam("pageSize") int pageSize,
            @RequestBody GetRoadPCIAndPQIResponse road ) {
        //获取道路类型
        String roadType = complainService.getRoadType2(road.getRoadName());
        if (roadType == null){
            roadType = complainService.getRoadType(road.getRoadName());
        }
        if (!(roadType == null)) {
            //智慧决策道路病害详情弹窗
            return complainService.roadCorrelation(road.getRoadName(), roadType, pageNumber, pageSize);
        }else {
            return new RoadDistressInformation();
        }
    }

    @PostMapping("/test")
    public Long test() {
        //新建数据投诉返回值集合
        //前往Mongo获取数据并将获取得数据进行封装
        return bridgeInfoDao.getRoadCorrelationCount();

    }


    /**
     * 投诉接入
     * @return
     */
    @GetMapping("/report")
    public ComplaintResponse getComplain( @RequestParam("pageNumber") int pageNumber,
                                          @RequestParam("pageSize") int pageSize,@RequestParam("year") int year,
                                          @RequestParam("month") int month) {
        ZhongHai zhongHai = new ZhongHai();
        zhongHai.setType(ZH_METHOD_COMPLAIN_REPORT);
        zhongHai.setArg("{\"year\":\"" + year + "\"}");
        ComplaintResponse complaintReport = complainService.getComplaintReport(zhongHai,pageNumber,pageSize,year,month);
        return complaintReport;
    }
}
